/* (C) fruitbatinshades 2008 onwards 
* <script type="text/javascript">
	$(document).ready(
		function()
		{
			$("div.fbisCarousel").abCarousel({noToDisplay:5,arrowSelector:'div.extraarrows a'});
		}
	);
	</script>
	<p>Simple carousel that adjusts itself to to width of the children.</p>
	<div class="fbisCarousel">
		<div class="arrows">
			<a class="slideright" href="#"><span>&lt;</span></a>
			<a class="slideleft" href="#"><span>&gt;</span></a>
		</div>
		<ul>
			<li><img src="images/1.png" /></li>
			<li><img src="images/2.png" /></li>
			<li><img src="images/3.png" /></li>
			<li><img src="images/4.png" /></li>
			<li><img src="images/5.png" /></li>
			<li><img src="images/6.png" /></li>
			<li><img src="images/7.png" /></li>
			<li><img src="images/8.png" /></li>
			<li><img src="images/9.png" /></li>
			<li><img src="images/10.png" /></li>
		</ul>
	</div>
    
    
    Change logs
    Nov 27, 2011 - trungdt - Fix bug: settings must be set to url property
*/

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};


jQuery.fn.abCarousel = function(options) {
  // set up initial options
  settings = jQuery.extend({
	 noToDisplay: 4, //no of items to display across
	 noToScroll: 4, //no of items to display across
	 adjustWidth: true, //whether to adjust the parent width on init
	 arrowSelector: null,  //use links outside our rendered html (extra buttons)
  }, options);
// iterate and initialise each matched element
  return this.each(function() {
	var $this = jQuery(this);
	var $items = $this.find("ul li")
	
	if (settings.adjustWidth)
	{
		//images aren't available to get their width till the doc is loaded
		//so we ned to waitl till then to do the resize.
		$(window).load(function(){
			//set all items to be the same width as the first
			//$items.width($items.eq(0).outerWidth());
			//set parent width to match items
			$this.width(($items.eq(0).outerWidth() * settings.noToDisplay)+2);
			//set arrow holder width to match parent div
			//$this.find(".arrows").width($this.outerWidth());
		});
	}
    $this.attr("arrowSelector", settings.arrowSelector);
	//extra arrows so bind then too
	if (settings.arrowSelector != null)
	{
		jQuery(settings.arrowSelector).click(function(e){scroll(e,$this,settings.noToScroll);});
	}
  });
  function scroll(e, car, swidth)
	{
		var ul = jQuery(car.find("ul").get(0));
		var noOff = ul.find("li").size();
		//var index = parseInt(jQuery.data(ul,"index"));
		var index = parseInt(jQuery(ul).data("index"));
		var left = parseInt(ul.css('left'));
        var arrowSelector=car.attr("arrowSelector");
		
		//get the relevant widths
		itemwidth = car.find("ul li").eq(0).outerWidth();
		noDisplayed = Math.round(car.outerWidth() / itemwidth);
		
		if (isNaN(index))
		{
			//not yet set
			jQuery(ul).data("index",1);
			index=0;
		}
		
		if ($(e.currentTarget).hasClass('slideleft'))
		{
			if(index > 0) //make sure we are not at the start
			{
				index-=swidth;
				if (index<1)
					index=0;
				jQuery(ul).data("index",index);
				//move the ul
				ul.animate({left:(0 - (index * itemwidth)) + 'px'}, 1500, 'easeOutQuart');
			}
		}
		if ($(e.currentTarget).hasClass('slideright'))
		{
			if(index + noDisplayed < (noOff )) // make sure we are not at the last item
			{
				index+=swidth;
				if (index > noOff)
					index=noOff;
				jQuery(ul).data("index",index);
				//move the ul
				ul.animate({left:(0 - (index * itemwidth)) + 'px'}, 1500, 'easeOutQuart');
			}
		}
		
		if (index == 0)
				jQuery(arrowSelector+".slideleft").addClass('disabled');
			else
				jQuery(arrowSelector+".slideleft").removeClass('disabled');
		
		
		if(index + noDisplayed < (noOff )) 
				jQuery(arrowSelector+".slideright").removeClass('disabled');
			else
				jQuery(arrowSelector+".slideright").addClass('disabled');
		
		//stop event
		e.preventDefault();
		return false;
	}
};
